package com.example.demo.algorithm.linear;

import java.util.Iterator;

/**
 * @Author: xiatian
 * @CreateTime: 2021/9/15 10:13 上午
 * @Description:
 */
public class Josephor {
    public static void main(String[] args) {
        Node<Integer> first = null;
        Node<Integer> pre = null;
        for (int i = 1; i <= 41; i++) {
            if (i == 1) {
                first = new Node<>(i, null);
                pre = first;
                continue;
            }
            Node newNode = new Node<>(i, null);
            pre.next = newNode;
            pre = newNode;
            if (i == 41) {
                pre.next = first;
            }
        }
        int count = 0;
        Node<Integer> n = first;
        Node<Integer> before = null;
        while (n != n.next) {
            count++;
            if (count == 3) {
                before.next = n.next;
                System.out.print(n.item + ",");
                n = n.next;
                count = 0;
            } else {
                before = n;
                n = n.next;
            }
        }
        System.out.println(n.item);
    }


    private static class Node<T> {
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

}
